var ajaxLayer;
function ajax(options) {
  ajaxLayer = layui.layer;
  var loadingLayer = layui.layer.load(1, { shade: [0.1, "#000"] });
  var postLoginUrl = options.url;
  var postData = options.data;
  var processData = true;
  var contentType = "application/json";
  postData = JSON.stringify(options.data);
  $.ajax({
    url: window.baseUrl + postLoginUrl,
    type: (options.type || "GET").toUpperCase(),
    timeout: options.timeout || 6000,
    processData: processData,
    data: postData,
    contentType: contentType,
    error: function (XMLHttpRequest) {
      if (XMLHttpRequest.status === 500) {
        ajaxLayer.confirm("未知错误：" + XMLHttpRequest.responseJSON.message, {
          btn: "确定",
          title: "系统异常",
          btnAlign: "c",
        });
      } else if (XMLHttpRequest.status === 403) {
        toLoginPage();
      } else if (XMLHttpRequest.statusText === "timeout") {
        ajaxLayer.confirm("请求超时", {
          btn: "确定",
          title: "请求错误",
          btnAlign: "c",
        });
      } else {
        ajaxLayer.confirm("系统出现异常，请联系系统管理员", {
          btn: "确定",
          title: "请求错误",
          btnAlign: "c",
        });
      }
      options.error && options.error(XMLHttpRequest);
    },
    success: function (res) {
      var status = res.status;
      setTimeout(function () {
        layui.layer.close(loadingLayer);
      }, 200);
      if (status === 200) {
        if (res.code === 0) {
          options.success && options.success(res);
        } else if (res.status === 13) {
          ajaxLayer.confirm(res.message, {
            btn: "确定",
            title: "提示",
            btnAlign: "c",
          });
          options.error && options.error(res);
        } else {
          ajaxLayer.confirm(res.message, {
            btn: "确定",
            title: "提示",
            btnAlign: "c",
          });
          options.error && options.error(res);
        }
      } else {
        if (status === 500) {
          ajaxLayer.confirm(res.message, {
            btn: "确定",
            title: "提示",
            btnAlign: "c",
          });
        } else {
          ajaxLayer.confirm("系统异常", {
            btn: "确定",
            title: "提示",
            btnAlign: "c",
          });
        }
        options.error && options.error(res);
      }
    },
    complete: function (res) {
      layui.layer.close(loadingLayer);
    },
  });
}
